#region (c) 2014 THN Solutions LLC. - All rights reserved
/*
THN TELEPORT version 2

Copyright (c) 2014, THN Solutions LLC. ( www.ThnSolutions.com )
Author: Nguyễn, M. Hải                 ( www.Minh-Hai.com     )
All rights reserved.

This library is dual-licensed.
  + For commercial software, please obtain a commercial license from THN Solutions LLC.

  + For free software, this library is licensed under GPL version 3. A summary of GPLv3 is
    listed below. You should also find a copy of GPLv3 as file License_GPLv3.txt included
	with the source-code files.

    This program is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License version 3 as published by
    the Free Software Foundation  of the License.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program.  If not, see <http://www.gnu.org/licenses/>.
*/
#endregion
using System;
using System.IO;
using System.Reflection;
using System.Windows.Forms;

using Thn;
using Thn.Application.Utilities;
using Thn.Application.Configuration;
using Thn.Interface.Vcl;

using Thn.Communication.Teleporting;
using Thn.Communication.Teleporting.Logging;
using Thn.Communication.Teleporting.Settings;
using Thn.Communication.Teleporting.Connectors;
using Thn.Communication.Teleporting.Connectors.Remoting;
using Thn.Communication.Teleporting.Connectors.Loopback;

using Demo.Business;

namespace Demo
{
    public partial class fmClient : Form
    {
        #region Fields
        string mGateway = "ProductService";
        string mConfigFilename = "Teleport - Client Side.xml";
        #endregion

        #region Initialize
        public fmClient()
        {
            InitializeComponent();

        }
        #endregion

        #region Load
        private void fmMain_Load(object sender, EventArgs e)
        {
            //trace calling methods when logging
            Log.TraceCallStack = true;

            //extract connector names
            SimpleConfigurator configurator = new SimpleConfigurator();
            configurator.Load(mConfigFilename);
            for (int i = 0; i < configurator.Sections.Count; i++)
            {
                ConfigSection section = configurator.Sections[i];
                lstConnectors.Items.Add(section["Name"]);
            }
        }
        #endregion

        #region Connect
        private void btnConnect_Click(object sender, EventArgs e)
        {
            if (lstConnectors.Text == string.Empty) MessageBox.Show("Please select a connector first", "Error", MessageBoxButtons.OK);
            else
            {
                string sectionName = lstConnectors.Text;

                TeleportConfigurator configurator = new TeleportConfigurator();
                configurator.AutoInitialize = false;//I don't want to initialize immediately
                ITeleportConnector connector = configurator.Load(mConfigFilename, lstConnectors.Text);

                #region Special treament for Loopback
                if (connector is LoopbackConnector)
                {
                    //special treatment in case of loop back because we haven't
                    //register a gateway yet. In other cases, this very application
                    //is a client, no need to register gateway at client side.
                    //In case of loopback, this app also acts as a gateway -> need to register
                    //a gateway
                    LoopbackConnector loopback = (LoopbackConnector)connector;
                    loopback.Initialized += new EventHandler(loopback_Initialized);
                }
                #endregion

                //initialize connector
                connector.Initialize();

                Log.Debug("Loaded");
            }
        }
        #endregion

        #region Loopback - Initialized
        void loopback_Initialized(object sender, EventArgs e)
        {
            //special treatment in case of loop back because we haven't
            //register a gateway yet. In other cases, this very application
            //is a client, no need to register gateway at client side.
            //In case of loopback, this app also acts as a gateway -> need to register
            //a gateway
            Teleport.AddGateway(mGateway, new Demo.Business.ProductService());
        }
        #endregion

        #region Ping
        private void btnPing_Click(object sender, EventArgs e)
        {
            string response = Teleport.Request(mGateway, "Ping") as string;
            if (response != null)
            {
                Log.Debug("Received response: {0}", response);
            }
            else
            {
                Log.Warning("Could not receive a response");
            }
        }
        #endregion

        #region Hello
        private void btnHello_Click(object sender, EventArgs e)
        {
            string response = Teleport.Request(mGateway, "Hello", "Minh Hai") as string;
            if (response != null)
            {
                Log.Debug("Received response: {0}", response);
            }
            else
            {
                Log.Warning("Could not receive a response");
            }
        }
        #endregion

        #region Get Random Product
        private void btnGetRandomProduct_Click(object sender, EventArgs e)
        {
            Product p = Teleport.Request(mGateway, "GetRandomProduct") as Product;
            if (p != null)
            {
                Log.Debug("{0}", p);
            }
            else
            {
                Log.Warning("Could not receive a response");
            }
        }
        #endregion

        #region Get Random Product List
        private void btnGetRandomProductList_Click(object sender, EventArgs e)
        {
            ProductList lst = Teleport.Request(mGateway, "GetRandomList") as ProductList;

            if (lst != null)
            {
                object scope = Log.Start(string.Format("Received list - {0} items", lst.Count));
                for (int i = 0; i < lst.Count; i++)
                {
                    Log.Debug("{0}", lst[i]);
                }
                Log.End(scope);
            }
            else
            {
                Log.Warning("Could not receive a response");
            }
        }
        #endregion

        #region Add Product
        private void btnAddProduct_Click(object sender, EventArgs e)
        {
            Product p = new Product();
            p.Name = "Apple";

            string response = Teleport.Request(mGateway, "AddProduct", p) as string;
            if (response != null)
            {
                Log.Debug("Received response: {0}", response);
            }
            else
            {
                Log.Warning("Could not receive a response");
            }
        }
        #endregion
    }
}